package net.wanho.service.impl;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.mapper.UserMapper;
import net.wanho.service.UserService;
import net.wanho.util.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author：汤小洋
 * Date：2024-01-05 11:39
 * Description：<描述>
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public void register(User user) {
        // 生成随机盐
        user.setSalt(SaltUtil.getSalt(8));
        // 加密
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), user.getSalt(), 1024);
        user.setPassword(md5Hash.toHex());
        // 保存用户
        userMapper.insert(user);

    }

    @Override
    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }
}
